package com.example.swim.controller;


import com.example.swim.Pojo.Activity;
import com.example.swim.Pojo.ClassRoom;
import com.example.swim.Pojo.News;
import com.example.swim.Pojo.Student;
import com.example.swim.Service.AcService;
import com.example.swim.Service.ClassRoomService;
import com.example.swim.Service.NewsService;
import com.example.swim.Service.StudentService;
import com.example.swim.Utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @author LiYa
 * @create 2022-07-29 9:59
 */
@RestController
@RequestMapping("/stu")
public class StudentController {
    @Autowired
    StudentService studentService;
    @Autowired
    AcService acService;
    @Autowired
    ClassRoomService classRoomService;
    @Autowired
    NewsService newsService;
//    登录检测
    @RequestMapping("/toMain")
    public R main(Student loginForm)
    {
        Student stu;
        HashMap<String,Object> map=new HashMap<>();
        if(studentService.findStu(loginForm)!=null)
        {
                stu=studentService.findStu(loginForm);
                map.put("user",stu);
                map.put("identity", "student");
                return new R(true,map);
        }
        return new R(false,"用户名或密码错误，请重新输入");
    }
//    进入主页面
    @PostMapping("/main")
    public R mainStu(Student student)
    {
        HashMap<String,Object> map=new HashMap<>();
        List<News> news=newsService.getAll();
        map.put("news", news);
//        查看是否报名了活动
        if(student.getAid()!=null)
        {
            Activity ac=acService.findAc(student.getAid());
            map.put("ac", ac);
        }
        ClassRoom cr=classRoomService.findByName(student.getCid());
        map.put("cr",cr);
        map.put("first",student.getFirst());
        return  new R(true,map);
    }
//   修改密码
    @PostMapping("/updatePwd")
    public R updatePwd(Student student)
    {
        //    更改数值，下次登录不需要更改密码
        studentService.updateFirst(student);
        studentService.updatePwd(student);
        HashMap<String,Object> map=new HashMap<>();
        map.put("user",student);
        map.put("identity","student");
        return new R(true,map);
    }
    @PostMapping("/updateSelf")
    public R update(Student student){
        studentService.update(student);
        return  new R(true,"修改个人信息成功");
    }
    // 退选活动
    @PostMapping("/quitA")
    public R quit1(Student student){

        Student stu=studentService.quitA(student);
        HashMap<String,Object> map=new HashMap<>();
        map.put("user",stu);
        map.put("identity","student");
        return  new R(true, map);
    }

//    退出
    @RequestMapping("/logout")
    public String logout(HttpSession session)
    {
        session.invalidate();
        return "redirect:/stu/log";
    }
}
